using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExploderBomb:MonoBehaviour {
	[SerializeField] float time;
	[SerializeField] Sprite[] animation;
	[SerializeField] float animationTime;
	[SerializeField] ProjectileData explosion;

	float timeThisFrame;
	int spriteIndex;
	SpriteRenderer spriteRenderer;

	private void Start() {
		spriteRenderer=GetComponent<SpriteRenderer>();
	}

	private void Update() {

		float timePerFrame = animationTime/animation.Length;

		timeThisFrame+=Time.deltaTime;
		if(timeThisFrame>timePerFrame) {
			timeThisFrame-=timePerFrame;
			spriteIndex=(spriteIndex+1)%animation.Length;
			spriteRenderer.sprite=animation[spriteIndex];
		}

		time-=Time.deltaTime;
		if(time<=0) {
			Explode();
			Destroy(gameObject);
		}
	}

	void Explode() {
		Projectile.Create(explosion,transform.position,Angle.right,null);
	}

}
